﻿using EFFC.Frame.Net.Base.Common;
using EFFC.Frame.Net.Base.Data.Base;
using EFFC.Frame.Net.Global;
using EFFC.Frame.Net.Module.HttpCall;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace XHRPMIS.Common
{
     public class FJKBAPI : SimpleRestCallHelper
{
    public FJKBAPI(string url = "", string access_secret = "")
    {
        ServerUrl = string.IsNullOrEmpty(url) ? MyConfig.GetConfiguration("FJKBAPI", "Server_Url") : url;
        AccessSecret = string.IsNullOrEmpty(access_secret) ? MyConfig.GetConfiguration("FJKBAPI", "Access_Secret") : access_secret;
    }
    /// <summary>
    /// 服务端连接
    /// </summary>
    public string ServerUrl
    {
        get; private set;
    }
    /// <summary>
    /// 访问远程API的Token
    /// </summary>
    public object AccessSecret { get; private set; }
    protected string Send(string url, object data, string method)
    {
        FrameDLRObject content = FrameDLRObject.CreateInstance(data, EFFC.Frame.Net.Base.Constants.FrameDLRFlags.SensitiveCase);
        var result = base.Send(url, data, new
        {
            Authorization = $"Bearer {AccessSecret}"
        }, method);
        GlobalCommon.Logger.WriteLog(EFFC.Frame.Net.Base.Constants.LoggerLevel.DEBUG, $"FJKBAPI：\nRequest url:{method} {url};Response Result={result}");
        if (result.StartsWith("Failed:")) result = "";
        return result;
    }
    protected string Get(string url)
    {
        var result = base.Get(url, new
        {
            Authorization = $"Bearer {AccessSecret}"
        });
        GlobalCommon.Logger.WriteLog(EFFC.Frame.Net.Base.Constants.LoggerLevel.DEBUG, $"FJKBAPI：\nRequest url:Get {url};Response Result={result}");
        if (result.StartsWith("Failed:")) result = "";
        return result;
    }
    /// <summary>
    /// 根据模板创建新的文档
    /// </summary>
    /// <param name="server_url"></param>
    /// <returns></returns>
    public bool CreateDoc(string template_id_or_title,
        string title,
        string version,
        string author,
        string desc,
        string key,
        string category,
        string trade,
        object customize_data,
        string add_id,
        string add_name,
        ref string msg,
        ref string doc_id)
    {
        var result = Send(ServerUrl + "/doc/template/create", new
        {
            template_id_or_title,
            data = new
            {
                title,
                version,
                author,
                desc,
                key,
                category,
                trade,
                customize_data,
                add_id,
                add_name
            }
        }, "Post");
        dynamic dobj = FrameDLRObject.IsJsonThen(result, null, EFFC.Frame.Net.Base.Constants.FrameDLRFlags.SensitiveCase);
        if (dobj == null)
        {
            msg = "访问出错";
            return false;
        }
        if (dobj.error != null)
        {
            msg = dobj.error;
            return false;
        }
        if (dobj != null && dobj.result != null)
        {
            if (dobj.result.code == "success")
            {
                doc_id = ComFunc.nvl(dobj.result.data.doc_id);
                return true;
            }
            else
            {
                msg = dobj.result.msg;
                return false;
            }
        }
        else
        {
            msg = "访问出错";
            return false;
        }
    }

    /// <summary>
    /// 根据模板创建新的版本
    /// </summary>
    /// <param name="template_id_or_title"></param>
    /// <param name="version"></param>
    /// <param name="author"></param>
    /// <param name="desc"></param>
    /// <param name="customize_data"></param>
    /// <param name="add_id"></param>
    /// <param name="add_name"></param>
    /// <param name="doc_id"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public bool CreateNewVersionForDocID(string template_id_or_title,
        string version,
        string author,
        string desc,
        object customize_data,
        string add_id,
        string add_name,
        string doc_id,
        ref string msg)
    {
        var result = Send(ServerUrl + $"/doc/{doc_id}/newversion", new
        {
            template_id_or_title,
            data = new
            {
                version,
                author,
                desc,
                customize_data,
                add_id,
                add_name
            }
        }, "Patch");
        dynamic dobj = FrameDLRObject.IsJsonThen(result, null, EFFC.Frame.Net.Base.Constants.FrameDLRFlags.SensitiveCase);
        if (dobj == null)
        {
            msg = "访问出错";
            return false;
        }
        if (dobj.error != null)
        {
            msg = dobj.error;
            return false;
        }
        if (dobj != null && dobj.result != null)
        {
            if (dobj.result.code == "success")
            {
                doc_id = ComFunc.nvl(dobj.result.data.doc_id);
                return true;
            }
            else
            {
                msg = dobj.result.msg;
                return false;
            }
        }
        else
        {
            msg = "访问出错";
            return false;
        }
    }
}
}
